Skip to content

fix(cli): reload MCP config after project selection - #966

Open
luantaraschi wants to merge 3 commits into
CodebuffAI:mainfrom
luantaraschi:fix/reload-mcp-after-project-change
Open

fix(cli): reload MCP config after project selection#966
luantaraschi wants to merge 3 commits into
CodebuffAI:mainfrom
luantaraschi:fix/reload-mcp-after-project-change

Conversation

@luantaraschi

Copy link
Copy Markdown

Summary

  • Reload the local agent registry after the project picker changes the working directory, so the selected project's .agents/mcp.json is loaded before the client is recreated.
  • Preserve the existing --agent override behavior.
  • Add a regression test that verifies a project MCP server reaches the base agent definition after project selection.

Fixes #957

Testing

  • Project picker and local agent registry tests: 41 passed
  • CLI typecheck
  • SDK build
  • Freebuff Windows binary build
  • Freebuff version and help E2E tests: 5 passed

Copilot AI lite review requested due to automatic review settings August 10, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chrismindpower369

Copy link
Copy Markdown

This looks like the stronger fix for #957 because the regression test exercises the actual activateProject() flow and verifies that the selected project's MCP server reaches the base agent definition.

PR #973 appears to address the same root cause with overlapping changes. Before merging, could we confirm that this PR also clears every derived local-agent cache that depends on the previous cwd/project root? If yes, I would suggest merging #966 and closing #973 as a duplicate to avoid two competing fixes.

@luantaraschi

Copy link
Copy Markdown
Author

Thanks for catching this. The local agent registry has two derived caches that depend on the previous working directory: cachedAgentsByMode and cachedAgentsDir. The new reloadLocalAgentRegistry() helper clears both before initializeAgentRegistry() refreshes the agent definitions, file paths, and MCP configuration.

I also expanded the regression test to warm the caches in the launch project, switch projects through activateProject(), and verify that the selected project supplies the agent directory, local agent list, MCP server, and base agent definition. The old project's agent must no longer be present.

Validation on the new head (2f246ab):

  • 41 related tests passed
  • CLI typecheck passed
  • Freebuff Windows binary build passed
  • 5 --version and --help E2E tests passed
  • Prettier and git diff --check passed

This incorporates the cache-reset part of #973 while keeping the project-picker coverage in this PR.

@chrismindpower369

Copy link
Copy Markdown

Perfect, thanks for the thorough follow-up. Clearing both cwd-dependent caches and testing the full activateProject() transition addresses the concern. The added assertion that the old project's agent is absent is especially valuable.

This now seems like the right PR to merge for #957. I’d suggest closing #973 as a duplicate once a maintainer has reviewed this update.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and clean fix. The root cause is real: handleProjectChange in cli/src/index.tsx previously called setProjectRoot/resetCodebuffClient but never re-scanned .agents/ for the newly selected directory, so cachedAgentsDir/cachedAgentsByMode in local-agent-registry.ts kept serving the launch directory's MCP config. Wrapping the chdir/setProjectRoot/registry-reload/reset sequence into a single activateProject() helper in project-picker.ts is a sensible consolidation and removes the duplicated logic from index.tsx.

The added test in project-picker.test.ts is a genuine regression test — it writes real .agents/*.ts and mcp.json files into temp launch/project directories, calls activateProject, and asserts the base agent definition picks up the new project's MCP server while dropping the launch project's local agent. That's exactly the kind of test this bug needed.

Two things worth double-checking before porting:

  1. reloadLocalAgentRegistry only clears cachedAgentsByMode and cachedAgentsDir — please confirm there isn't a third cache (e.g. for MCP servers specifically, or directory-scan results) elsewhere in local-agent-registry.ts that also needs clearing, since getLoadedMCPServers() presumably reads from one of these.
  2. The hasAgentOverride skip path (reloadAgentRegistry: !hasAgentOverride) isn't exercised by the new test — a second test case confirming the override path doesn't reload would strengthen this further, though the existing description of preserving that behavior is plausible from the diff.

No forbidden paths touched, no scope creep — solid, focused bug fix.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Aug 20, 2026
@luantaraschi

Copy link
Copy Markdown
Author

Both checked.

On the first one, there is no third cache. The module keeps five mutable ones: userAgentsCache, userAgentFilePaths and mcpServersCache at the top, and cachedAgentsDir and cachedAgentsByMode further down. initializeAgentRegistry() reassigns the first three on both the success and the failure path, including mcpServersCache from loadMCPConfigSync(), which reads the cwd, so getLoadedMCPServers() is covered as long as the chdir happens first. That is why reloadLocalAgentRegistry() only clears the other two: they are derived and nothing else resets them. __resetLocalAgentRegistryForTests lists all five, which is a handy way to confirm the count did not grow.

On the second one, you were right that the skip path had no coverage. Added a test for it: with { reloadAgentRegistry: false } the chdir and project root still move, and the launch project's agent is still the one served. I checked it is worth something by making activateProject reload unconditionally, and the new test is the only one that fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MCP servers from .agents/mcp.json are not loaded when the project is selected via the project picker (CLI started from an ancestor directory)

4 participants